package lp.xian.equipment.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lp.xian.equipment.domain.Finance;
import lp.xian.equipment.mapper.FinanceMapper;
import lp.xian.equipment.service.IFinanceService;
import lp.xian.equipment.vo.FinanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 财务数据Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-30
 */
@Service
public class FinanceServiceImpl implements IFinanceService
{
    @Autowired
    private FinanceMapper financeMapper;

    /**
     * 查询财务数据
     *
     * @param financeId 财务数据主键
     * @return 财务数据
     */
    @Override
    public Finance selectFinanceByFinanceId(Integer financeId)
    {
        return financeMapper.selectFinanceByFinanceId(financeId);
    }

    /**
     * 查询财务数据列表
     *
     * @param finance 财务数据
     * @return 财务数据
     */
    @Override
    public List<Finance> selectFinanceList(Finance finance)
    {
        return financeMapper.selectFinanceList(finance);
    }

    /**
     * 新增财务数据
     *
     * @param finance 财务数据
     * @return 结果
     */
    @Override
    public int insertFinance(Finance finance)
    {
        finance.setCreateTime(DateUtils.getNowDate());
        return financeMapper.insertFinance(finance);
    }

    /**
     * 修改财务数据
     *
     * @param finance 财务数据
     * @return 结果
     */
    @Override
    public int updateFinance(Finance finance)
    {
        finance.setUpdateTime(DateUtils.getNowDate());
        return financeMapper.updateFinance(finance);
    }

    /**
     * 批量删除财务数据
     *
     * @param financeIds 需要删除的财务数据主键
     * @return 结果
     */
    @Override
    public int deleteFinanceByFinanceIds(Integer[] financeIds)
    {
        return financeMapper.deleteFinanceByFinanceIds(financeIds);
    }

    /**
     * 删除财务数据信息
     *
     * @param financeId 财务数据主键
     * @return 结果
     */
    @Override
    public int deleteFinanceByFinanceId(Integer financeId)
    {
        return financeMapper.deleteFinanceByFinanceId(financeId);
    }

    @Override
    public int auditFinance(FinanceVo financeVo) {
        Finance finance=new Finance();
        finance.setFinanceId(financeVo.getFinanceId());
        finance.setAuditOpinion(financeVo.getAuditOpinion());
        finance.setAuditStatus(financeVo.getAuditStatus());
        finance.setAuditPerson(SecurityUtils.getUsername());
        finance.setAuditTime(new Date());
        return financeMapper.updateFinance(finance);
    }

    @Override
    public int approverFinance(FinanceVo financeVo) {
        //审批后数据需要提交到财务 收入支出
        Finance finance=new Finance();
        finance.setFinanceId(financeVo.getFinanceId());
        finance.setAuditStatus(financeVo.getAuditStatus());
        finance.setApproverOpinion(financeVo.getApproverOpinion());
        finance.setApproverTime(new Date());
        finance.setApproverPerson(SecurityUtils.getUsername());
        int num=financeMapper.updateFinance(finance);//审批操作
        if (num>0){
           Finance yu=financeMapper.selectFinanceBalance();//查余额
            Finance finance1=financeMapper.selectFinanceByFinanceId(financeVo.getFinanceId());//查出对象
            if (finance1.getAuditStatus().intValue()==2) {
                double financeBalance=yu.getFinanceBalance().doubleValue();//获取余额数据
                double financeMoney=finance1.getFinanceMoney().doubleValue();//发生额数据
                if (finance1.getFinanceStatus().intValue()==0) {
                    double nums = financeBalance - financeMoney;//减掉余额赋值发生额
                    BigDecimal financeBalance1 = new BigDecimal(nums);
                    finance1.setFinanceBalance(financeBalance1);
                    return financeMapper.updateFinance(finance1);
                }else if (finance1.getFinanceStatus().intValue()==1){
                    double nums = financeBalance + financeMoney;//减掉余额赋值发生额
                    BigDecimal financeBalance1 = new BigDecimal(nums);
                    finance1.setFinanceBalance(financeBalance1);
                    return financeMapper.updateFinance(finance1);
                }
            }



        }
        return 1;
    }

    @Override
    public List<Finance> CopeFinanceList(Finance finance) {
        return financeMapper.CopeFinanceList(finance);
    }
}
